package com.binvshe.binvshe.contacts;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.pachong.android.baseuicomponent.activity.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 搜索好友的界面
 * 1. 添加好友->搜索
 * 2. 好友列表 -> 搜索
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-10--26
 * @Version: 1.0.0
 */
public class SearchFriendActivity extends BaseActivity {
    public static final String KEY_FLAG = "key_flag";
    public static final int FLAG_ADD_FRIEND = 1;
    public static final int FLAG_MY_FRIEND = 2;
    int flag = FLAG_MY_FRIEND;


    SearchResultFragment menuFrag;

    @Bind(R.id.etInput)
    EditText etInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        flag = getIntent().getIntExtra(KEY_FLAG, FLAG_ADD_FRIEND);
        hideToolbar();
        menuFrag = SearchResultFragment.newInstance(flag);
        getSupportFragmentManager().beginTransaction().add(R.id.lytSearchResult, menuFrag).commit();

        etInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                /*判断是否是“GO”键*/
                if (actionId == EditorInfo.IME_ACTION_SEARCH||actionId== EditorInfo.IME_ACTION_DONE) {
                    String searchKey = etInput.getText().toString().trim();
                    if (!TextUtils.isEmpty(searchKey)) {
                        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        im.hideSoftInputFromWindow(etInput.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                        menuFrag.onSearch(searchKey);
                    }
                    return true;
                }
                return false;
            }
        });

    }

    @OnClick(R.id.btnCancel)
    public void onBtnCancleClicked() {
        finish();
    }

    @Override
    public View createDataView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.activity_search_add_friends, null);
    }


    @Override
    public View createEmptyView(LayoutInflater inflater) {
        return super.createEmptyView(inflater);
    }


    public static void start(Context context, int flag) {
        Intent intent = new Intent(context, SearchFriendActivity.class);
        intent.putExtra(KEY_FLAG, flag);
        context.startActivity(intent);
    }
}
